//
//  HttpClient.m
//  tingCheChang
//
//  Created by hff on 15/8/3.
//  Copyright (c) 2015年 threeTi. All rights reserved.
//

#import "DNPersonHttpsClient.h"
#import "NSMutableDictionary+Utils.h"
#import "NSString+Category.h"


#define REQUEST_ADDRESS  @""



@implementation DNPersonHttpsClient
static DNPersonHttpsClient *instance = nil;


//单例
+(DNPersonHttpsClient *)shareInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DNPersonHttpsClient alloc] init];
    });
    
    return instance;
}


 
#pragma mark - 登陆注册

//登录
- (void)requestUserLoginWithCellNumber:(NSString *)cellNumber
                              password:(NSString *)password
                               success:(TTIClientManagerBlock)success
                               failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
//    [params addUnEmptyString:password      forKey:@"password"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@""];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
        
    }];
}


//注册
- (void)requestUserRegistWithCellNumber:(NSString *)cellNumber
                                   code:(NSString *)code
                               password:(NSString *)password
                                success:(TTIClientManagerBlock)success
                                failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
    [params addUnEmptyString:code      forKey:@"code"];
    [params addUnEmptyString:password      forKey:@"password"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/regist"];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];
}


//验证码
//- (void)requestVerifyPhoneCodeWithCellNumber:(NSString *)cellNumber
//                                     success:(TTIClientManagerBlock)success
//                                     failuer:(TTIClientManagerBlock)failure
//{
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
//    
//    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/sendVerifyCode"];
//    
//    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//    }];
//}


//完善个人信息
- (void)requestFinishUserInfoWithCellNumber:(NSString *)cellNumber
                                  headPhoto:(UIImage *)headPhoto
                                   nickName:(NSString *)nickName
                                     gender:(NSString *)gender
                                    country:(NSString *)country
                                    success:(TTIClientManagerBlock)success
                                    failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
    [params addUnEmptyString:nickName    forKey:@"nickName"];
    [params addUnEmptyString:gender    forKey:@"gender"];
    [params addUnEmptyString:country    forKey:@"country"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/completeInfo"];
    
    [self.manager POST:URLstring parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formDta) {
        
        NSData *imageData = UIImageJPEGRepresentation(headPhoto, 0.8);
        if (imageData)
        {
            [formDta appendPartWithFileData:imageData name:@"headPhoto" fileName:@"headPhoto.jpg" mimeType:@"image/jpeg"];
        }
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
        
    }];
}


//验证验证码
//- (void)requestVerifyCodeWithCellNumber:(NSString *)cellNumber
//                                   verityCode:(NSString *)verityCode
//                                success:(TTIClientManagerBlock)success
//                                failuer:(TTIClientManagerBlock)failure
//{
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
//    [params addUnEmptyString:verityCode    forKey:@"verityCode"];
//    
//
//    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/checkVerifyCode"];
//    
//    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//    }];
//}


//重置密码
- (void)requestResetPasswordWithCellNumber:(NSString *)cellNumber
                                   code:(NSString *)code
                                 newPwd:(NSString *)newPwd
                                success:(TTIClientManagerBlock)success
                                failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
    [params addUnEmptyString:code    forKey:@"code"];
    [params addUnEmptyString:newPwd    forKey:@"newPwd"];
    
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/resetNewPwdByCode"];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];
}


#pragma mark - 国家列表
- (void)requestCountryListWithNextPage:(NSString *)nextPage
                              pageSize:(NSString *)pageSize
                               success:(TTIClientManagerBlock)success
                               failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:nextPage    forKey:@"nextPage"];
    [params addUnEmptyString:pageSize    forKey:@"pageSize"];
   
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/country/findCountryList"];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];
}


#pragma mark - 意见反馈
//意见反馈
- (void)requestAddFeedbackWithMemberId:(NSString *)memberId
                              feedtype:(NSString *)feedtype
                               content:(NSString *)content
                           contactInfo:(NSString *)contactInfo
                               success:(TTIClientManagerBlock)success
                               failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:memberId    forKey:@"memberId"];
    [params addUnEmptyString:feedtype    forKey:@"feedtype"];
    [params addUnEmptyString:content    forKey:@"content"];
    [params addUnEmptyString:contactInfo    forKey:@"contactInfo"];
    
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/feedback/sendSuggestion"];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];
}


//版本更新
- (void)requestUpdateVersionWithUserDevice:(NSString *)userDevice
                                appVersion:(NSString *)appVersion
                                   success:(TTIClientManagerBlock)success
                                   failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:userDevice    forKey:@"userDevice"];
    [params addUnEmptyString:appVersion    forKey:@"appVersion"];
   
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/appVersion/findUpdateInfo"];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];
}


//消息列表
- (void)requestMessageListWithNextPage:(NSString *)nextPage
                              pageSize:(NSString *)pageSize
                               success:(TTIClientManagerBlock)success
                               failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:nextPage    forKey:@"nextPage"];
    [params addUnEmptyString:pageSize    forKey:@"pageSize"];
    
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/message/findMessageList"];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];

}


//消息详情
- (void)requestMessageDetialWithModelId:(NSString *)modelId
                                success:(TTIClientManagerBlock)success
                                failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:modelId    forKey:@"modelId"];
    
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/message/findMessageById"];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];

}
//公告信息
- (void)requestNoticeListWithNextPage:(NSString *)nextPage
                             pageSize:(NSString *)pageSize
                               status:(NSString *)status
                             sortData:(NSString *)sortData
                               userId:(NSString *)userId
                              success:(TTIClientManagerBlock)success
                              failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:nextPage    forKey:@"nextPage"];
    [params addUnEmptyString:pageSize    forKey:@"pageSize"];
    [params addUnEmptyString:status      forKey:@"status"];
//    [params addUnEmptyString:sortData    forKey:@"sortData"];
    [params addUnEmptyString:userId      forKey:@"userId"];
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/announcement/findAnnouncementList"];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];
}
//公告详情
- (void)requestNoticeDetailWithId:(NSString *)modelId
                           userId:(NSString *)userId
                          success:(TTIClientManagerBlock)success
                          failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:modelId    forKey:@"modelId"];
    [params addUnEmptyString:userId    forKey:@"userId"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/announcement/findAnnouncementById"];
    
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];
    
}
#pragma mark - 查询手机号是否已注册
- (void)requestCheckCellNumberRegistedWithcellNumber:(NSString *)cellNumber
                                  success:(TTIClientManagerBlock)success
                                  failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
    
     NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/phoneRegistCheck"];
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];
}

#pragma mark - 添加飞行记录
- (void)requestSaveFlyRecordWithName:(NSString *)name
                             flyDate:(NSString *)flyDate
                              userId:(NSString *)userId
                             success:(TTIClientManagerBlock)success
                             failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:name    forKey:@"name"];
    [params addUnEmptyString:flyDate    forKey:@"flyDate"];
    [params addUnEmptyString:userId    forKey:@"userId"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/firmware/saveFlyRecord"];
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];

}

#pragma mark - 查询飞行记录
- (void)requestFindFlyRecordListNextPage:(NSString *)nextPage
                                pageSize:(NSString *)pageSize
                              jsonFilter:(NSString *)jsonFilter
                                 success:(TTIClientManagerBlock)success
                                 failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:nextPage    forKey:@"nextPage"];
    [params addUnEmptyString:pageSize    forKey:@"pageSize"];
    
    //jsonFilter控制器传过来 userId,用userId拼接jsonFilter对象
    NSString *jsonKey = @"search_EQ_createUser";
    NSString *json = [NSString stringWithFormat:@"{'%@':'%@'}",jsonKey,jsonFilter];
    [params addUnEmptyString:json forKey:@"jsonFilter"];

    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/firmware/findFlyRecordList"];
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];

}
#pragma mark - 查询是否有新固件
- (void)requestFindUpdateInfoWithType:(NSString *)type
                      firmwareVersion:(NSString *)firmwareVersion
                              success:(TTIClientManagerBlock)success
                              failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:type    forKey:@"type"];
    [params addUnEmptyString:firmwareVersion    forKey:@"firmwareVersion"];

    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/firmware/findUpdateInfo"];
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];

}

#pragma mark - 新增固件
- (void)requestSaveFirmWareWithName:(NSString *)name
                        instruction:(NSString *)instruction
                           location:(NSString *)location
                             userId:(NSString *)userId
                            success:(TTIClientManagerBlock)success
                            failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:name    forKey:@"name"];
    [params addUnEmptyString:instruction    forKey:@"instruction"];
    [params addUnEmptyString:location    forKey:@"location"];
    [params addUnEmptyString:userId    forKey:@"userId"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/firmware/saveFirmware"];
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];

}
#pragma mark - 我的消息小红点
- (void)requestCheckNewMsgWithuserId:(NSString *)userId
                             success:(TTIClientManagerBlock)success
                             failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:userId    forKey:@"userId"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/announcement/findNewAnnouncement"];
    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];

}

#pragma mark - app下载
- (void)requestDownloadNewAppsuccess:(TTIClientManagerBlock)success
                             failure:(TTIClientManagerBlock)failure
{
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/appVersion/appDownload"];
    [self.manager GET:URLstring parameters:nil success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
        
        [self parseResponse:responseObject operation:operation success:success failure:failure];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
    }];

}
@end
